home *** CD-ROM | disk | FTP | other *** search
- package com.sun.java.swing.text;
-
- import com.sun.java.swing.Action;
- import com.sun.java.swing.JComponent;
- import com.sun.java.swing.SwingUtilities;
- import com.sun.java.swing.Timer;
- import com.sun.java.swing.event.ChangeEvent;
- import com.sun.java.swing.event.ChangeListener;
- import com.sun.java.swing.event.EventListenerList;
- import com.sun.java.swing.plaf.TextUI;
- import java.awt.Component;
- import java.awt.Graphics;
- import java.awt.Point;
- import java.awt.Rectangle;
- import java.awt.event.ActionEvent;
- import java.awt.event.FocusEvent;
- import java.awt.event.FocusListener;
- import java.awt.event.MouseEvent;
- import java.awt.event.MouseListener;
- import java.awt.event.MouseMotionListener;
- import java.io.IOException;
- import java.io.ObjectInputStream;
- import java.io.Serializable;
-
- public class DefaultCaret implements Caret, Serializable, FocusListener, MouseListener, MouseMotionListener {
- protected EventListenerList listenerList = new EventListenerList();
- protected ChangeEvent changeEvent = null;
- JTextComponent component;
- boolean visible;
- int dot;
- int mark;
- Object selectionTag;
- Timer flasher;
- Point magicCaretPosition;
- transient UpdateHandler updateHandler = new UpdateHandler(this);
- static Class class$com$sun$java$swing$event$ChangeListener;
-
- public void addChangeListener(ChangeListener l) {
- EventListenerList var10000 = this.listenerList;
- Class var10001 = class$com$sun$java$swing$event$ChangeListener;
- if (var10001 == null) {
- try {
- var10001 = Class.forName("com.sun.java.swing.event.ChangeListener");
- } catch (ClassNotFoundException var2) {
- throw new NoClassDefFoundError(((Throwable)var2).getMessage());
- }
-
- class$com$sun$java$swing$event$ChangeListener = var10001;
- }
-
- var10000.add(var10001, l);
- }
-
- protected void adjustVisibility(Rectangle nloc) {
- SwingUtilities.invokeLater(new SafeScroller(this, nloc));
- }
-
- void changeCaretPosition(int dot) {
- TextUI mapper = this.component.getUI();
- Document doc = this.component.getDocument();
- if (mapper != null && doc != null) {
- try {
- Rectangle var8 = mapper.modelToView(this.dot);
- this.damage(var8);
- } catch (BadLocationException var7) {
- Rectangle oldLoc = null;
- }
-
- this.dot = dot;
-
- Rectangle newLoc;
- try {
- newLoc = mapper.modelToView(this.dot);
- } catch (BadLocationException var6) {
- newLoc = null;
- }
-
- if (newLoc != null) {
- this.adjustVisibility(newLoc);
- }
-
- this.damage(newLoc);
- this.fireStateChanged();
- }
-
- }
-
- protected void damage(Rectangle r) {
- if (r != null) {
- this.component.repaint(r.x - 1, r.y, 3, r.height);
- }
-
- }
-
- public void deinstall(JTextComponent c) {
- ((Component)c).removeMouseListener(this);
- ((Component)c).removeMouseMotionListener(this);
- ((Component)c).removeFocusListener(this);
- ((JComponent)c).removePropertyChangeListener(this.updateHandler);
- Document doc = c.getDocument();
- if (doc != null) {
- doc.removeDocumentListener(this.updateHandler);
- }
-
- this.component = null;
- if (this.flasher != null) {
- this.flasher.stop();
- }
-
- }
-
- protected void fireStateChanged() {
- Object[] listeners = this.listenerList.getListenerList();
-
- for(int i = listeners.length - 2; i >= 0; i -= 2) {
- Object var10000 = listeners[i];
- Class var10001 = class$com$sun$java$swing$event$ChangeListener;
- if (var10001 == null) {
- try {
- var10001 = Class.forName("com.sun.java.swing.event.ChangeListener");
- } catch (ClassNotFoundException var3) {
- throw new NoClassDefFoundError(((Throwable)var3).getMessage());
- }
-
- class$com$sun$java$swing$event$ChangeListener = var10001;
- }
-
- if (var10000 == var10001) {
- if (this.changeEvent == null) {
- this.changeEvent = new ChangeEvent(this);
- }
-
- ((ChangeListener)listeners[i + 1]).stateChanged(this.changeEvent);
- }
- }
-
- }
-
- public void focusGained(FocusEvent e) {
- if (this.component.isEditable()) {
- this.setVisible(true);
- }
-
- }
-
- public void focusLost(FocusEvent e) {
- this.setVisible(false);
- }
-
- public int getBlinkRate() {
- return this.flasher == null ? 0 : this.flasher.getDelay();
- }
-
- protected final JTextComponent getComponent() {
- return this.component;
- }
-
- public int getDot() {
- return this.dot;
- }
-
- public Point getMagicCaretPosition() {
- return this.magicCaretPosition;
- }
-
- public int getMark() {
- return this.mark;
- }
-
- protected Highlighter.HighlightPainter getSelectionPainter() {
- Highlighter.HighlightPainter p = new DefaultHighlighter.DefaultHighlightPainter(this.component.getSelectionColor());
- return p;
- }
-
- public void install(JTextComponent c) {
- this.component = c;
- Document doc = c.getDocument();
- this.dot = this.mark = 0;
- if (doc != null) {
- doc.addDocumentListener(this.updateHandler);
- }
-
- ((JComponent)c).addPropertyChangeListener(this.updateHandler);
- ((Component)c).addFocusListener(this);
- ((Component)c).addMouseListener(this);
- ((Component)c).addMouseMotionListener(this);
- }
-
- public boolean isSelectionVisible() {
- return this.selectionTag != null;
- }
-
- public boolean isVisible() {
- return this.visible;
- }
-
- public void mouseClicked(MouseEvent e) {
- if (e.getClickCount() == 2) {
- Action a = new DefaultEditorKit.SelectWordAction();
- a.actionPerformed((ActionEvent)null);
- } else if (e.getClickCount() == 3) {
- Action a = new DefaultEditorKit.SelectLineAction();
- a.actionPerformed((ActionEvent)null);
- }
-
- }
-
- public void mouseDragged(MouseEvent e) {
- this.moveCaret(e);
- }
-
- public void mouseEntered(MouseEvent e) {
- }
-
- public void mouseExited(MouseEvent e) {
- }
-
- public void mouseMoved(MouseEvent e) {
- }
-
- public void mousePressed(MouseEvent e) {
- this.positionCaret(e);
- if (this.component.isEnabled()) {
- this.component.requestFocus();
- }
-
- }
-
- public void mouseReleased(MouseEvent e) {
- }
-
- protected void moveCaret(MouseEvent e) {
- Point pt = new Point(e.getX(), e.getY());
- int pos = this.component.viewToModel(pt);
- if (pos >= 0) {
- this.moveDot(pos);
- }
-
- }
-
- public void moveDot(int dot) {
- if (dot != this.dot) {
- this.changeCaretPosition(dot);
- Highlighter h = this.component.getHighlighter();
- int p0 = Math.min(dot, this.mark);
- int p1 = Math.max(dot, this.mark);
-
- try {
- if (this.selectionTag != null) {
- h.changeHighlight(this.selectionTag, p0, p1);
- } else {
- Highlighter.HighlightPainter p = this.getSelectionPainter();
- this.selectionTag = h.addHighlight(p0, p1, p);
- }
- } catch (BadLocationException var6) {
- throw new StateInvariantError("Bad caret position");
- }
- }
-
- }
-
- public void paint(Graphics g) {
- if (this.isVisible()) {
- try {
- TextUI mapper = this.component.getUI();
- Rectangle r = mapper.modelToView(this.dot);
- g.setColor(this.component.getCaretColor());
- g.drawLine(r.x, r.y, r.x, r.y + r.height - 1);
- } catch (BadLocationException var4) {
- }
- }
-
- }
-
- protected void positionCaret(MouseEvent e) {
- Point pt = new Point(e.getX(), e.getY());
- int pos = this.component.viewToModel(pt);
- if (pos >= 0) {
- this.setDot(pos);
- this.setMagicCaretPosition((Point)null);
- }
-
- }
-
- private void readObject(ObjectInputStream s) throws ClassNotFoundException, IOException {
- s.defaultReadObject();
- this.updateHandler = new UpdateHandler(this);
- }
-
- public void removeChangeListener(ChangeListener l) {
- EventListenerList var10000 = this.listenerList;
- Class var10001 = class$com$sun$java$swing$event$ChangeListener;
- if (var10001 == null) {
- try {
- var10001 = Class.forName("com.sun.java.swing.event.ChangeListener");
- } catch (ClassNotFoundException var2) {
- throw new NoClassDefFoundError(((Throwable)var2).getMessage());
- }
-
- class$com$sun$java$swing$event$ChangeListener = var10001;
- }
-
- var10000.remove(var10001, l);
- }
-
- public void setBlinkRate(int rate) {
- if (rate != 0) {
- if (this.flasher == null) {
- this.flasher = new Timer(rate, this.updateHandler);
- }
-
- this.flasher.setDelay(rate);
- } else if (this.flasher != null) {
- this.flasher.stop();
- this.flasher.removeActionListener(this.updateHandler);
- this.flasher = null;
- }
-
- }
-
- public void setDot(int dot) {
- Document doc = this.component.getDocument();
- if (doc != null) {
- dot = Math.min(dot, doc.getLength());
- }
-
- dot = Math.max(dot, 0);
- this.mark = dot;
- if (this.dot != dot || this.selectionTag != null) {
- this.changeCaretPosition(dot);
- }
-
- if (this.selectionTag != null) {
- Highlighter h = this.component.getHighlighter();
- h.removeHighlight(this.selectionTag);
- this.selectionTag = null;
- }
-
- }
-
- public void setMagicCaretPosition(Point p) {
- this.magicCaretPosition = p;
- }
-
- public void setSelectionVisible(boolean vis) {
- if (vis) {
- if (this.selectionTag == null && this.dot != this.mark) {
- Highlighter h = this.component.getHighlighter();
- int p0 = Math.min(this.dot, this.mark);
- int p1 = Math.max(this.dot, this.mark);
- Highlighter.HighlightPainter p = this.getSelectionPainter();
-
- try {
- this.selectionTag = h.addHighlight(p0, p1, p);
- } catch (BadLocationException var6) {
- this.selectionTag = null;
- }
- }
- } else if (this.selectionTag != null) {
- Highlighter h = this.component.getHighlighter();
- h.removeHighlight(this.selectionTag);
- this.selectionTag = null;
- }
-
- }
-
- public void setVisible(boolean e) {
- TextUI mapper = this.component.getUI();
- Document doc = this.component.getDocument();
- if (this.visible != e && doc != null && mapper != null) {
- try {
- Rectangle loc = mapper.modelToView(this.dot);
- this.damage(loc);
- } catch (BadLocationException var5) {
- }
- }
-
- this.visible = e;
- if (this.flasher != null) {
- if (this.visible) {
- this.flasher.start();
- } else {
- this.flasher.stop();
- }
- }
-
- }
- }
-